package com.lxq.particledemo02;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.util.Log;

/**
 * 作者： lxq on 2017/7/10 17:20
 * 邮箱： 842202389@qq.com
 * 用途：
 */

public class TextureUtil {
    public static int loadTexture(Context context, int resourceId) {
        int[] textureIds = new int[1];
        GLES20.glGenTextures(1, textureIds, 0);
        if (textureIds[0] == 0) {
            Log.e("opengl", "cannot generate a new opengl texture");
            return 0;
        }

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);
        if (bitmap == null) {
            Log.e("bitmap", "cannot generate a bitmap");
            GLES20.glDeleteTextures(1, textureIds, 0);
        }


        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0]);//绑定纹理

        //设置纹理过滤Filter
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);//处理缩小情况，有三线过滤，效果=双线过滤+MIP贴图
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);//放大情况，使用双线过滤

        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();

        GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

        return textureIds[0];
    }
}
